package cn.exrick.xboot.modules.base.controller.manage;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.entity.Position;
import cn.exrick.xboot.modules.base.param.PositionParam;
import cn.exrick.xboot.modules.base.service.mybatis.IPositionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author njp
 */
@Slf4j
@RestController
@Api(description = "岗位管理接口")
@RequestMapping("/xboot/position")
@Transactional
public class PositionController {

    @Autowired
    private IPositionService iPositionService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Position> get(@PathVariable String id){

        Position position = iPositionService.getById(id);
        return new ResultUtil<Position>().setData(position);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Position>> getAll(){

        List<Position> list = iPositionService.list();
        return new ResultUtil<List<Position>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Position>> getByPage(@ModelAttribute PageVo page){

        IPage<Position> data = iPositionService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Position>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Position> saveOrUpdate(@ModelAttribute Position position){

        if(iPositionService.saveOrUpdate(position)){
            return new ResultUtil<Position>().setData(position);
        }
        return new ResultUtil<Position>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            // todo 检查该岗位是否被用户引用
            iPositionService.removeById(id);
            //todo 岗位与菜单中间表的数据 （因中间表逻辑未实现暂时先不做）

            //todo 缓存相关操作看后续优化时如果有时间进行优化，没时间不做缓存，直接操作数据库
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增岗位")
    public Result<Object> add(@Validated(PositionParam.add.class) @ModelAttribute PositionParam positionParam){
        iPositionService.add(positionParam);
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑岗位")
    public Result<Object> edit(@Validated(PositionParam.edit.class) @ModelAttribute PositionParam positionParam){
        iPositionService.edit(positionParam);
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

}
